package com.sevenGroup.pawnshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenGroup.pawnshop.Utils.CopyProperties;
import com.sevenGroup.pawnshop.Utils.GetJson;
import com.sevenGroup.pawnshop.Utils.GetObjectByJson;
import com.sevenGroup.pawnshop.Utils.TimeUtils;
import com.sevenGroup.pawnshop.controller.form.GoodsForm;
import com.sevenGroup.pawnshop.controller.form.ShopSelectConditionalForm;
import com.sevenGroup.pawnshop.controller.info.GoodsInfo;
import com.sevenGroup.pawnshop.controller.info.PropertiesInfo;
import com.sevenGroup.pawnshop.dao.*;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.*;
import com.sevenGroup.pawnshop.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements GoodsService {
    @Resource
    private GoodsDao goodsDao;

    @Resource
    private FirstTypeDao firstTypeDao;

    @Resource
    private SecondTypeDao secondTypeDao;

    @Resource
    private BrandDao brandDao;

    @Resource
    private StoresDao storesDao;

    //  新增商品
    @Override
    public Integer saveShop(GoodsForm goodsForm) {
//        先转换,并在新对象里生成提交时间，再将属性的list集合转成String字符串
        Goods goods = CopyProperties.copyOne(goodsForm, Goods.class);
        goods.setSubmitTime(TimeUtils.getNowTimeString());

//        通过门店ID查询门店名称
        Stores store = storesDao.selectOne(new QueryWrapper<Stores>().eq("store_id", goodsForm.getStoreId()).select("store_name"));
        goods.setStoreName(store.getStoreName());

//        List转String需要调用JSON工具
        goods.setGoodProperties(GetJson.getListJson("", goodsForm.getGoodProperties()));
        goods.setGoodLdentificationFigure(GetJson.getListJson("", goodsForm.getGoodLdentificationFigure()));
//        赋默认值
        goods.setGoodState("待鉴定");
//        调用dao
        int insert = goodsDao.insert(goods);
        return insert;
    }

    //  查询所有商品
    @Override
    public IPage<GoodsInfo> selectAllShops(Page page) {
        IPage pages = goodsDao.selectPage(page, new QueryWrapper<Goods>());
//        将pages中的集合类型转成info
        List<Goods> oldRecords = pages.getRecords();
        List<GoodsInfo> goodsInfos = CopyProperties.copyList(oldRecords, GoodsInfo.class);
//        遍历集合，将其中的属性数组和鉴定图数组传入
        for (GoodsInfo goodsInfo : goodsInfos) {
            for (Goods goods : oldRecords) {
                if (goods.getGoodId() == goodsInfo.getGoodId()) {
                    goodsInfo.setGoodLdentificationFigure(GetObjectByJson.getList("", goods.getGoodLdentificationFigure(), LdentificationFigur.class));
                    goodsInfo.setGoodProperties(GetObjectByJson.getList("", goods.getGoodProperties(), PropertiesInfo.class));
                }
            }
        }
        pages.setRecords(goodsInfos);
        return pages;
    }

    //  根据条件查询商品（门店ID，仓库ID，商品大类ID，小类ID，商品ID等）
    @Override
    public IPage<GoodsInfo> selectShopsByConditionals(ShopSelectConditionalForm selectConditionalForm) {
        IPage pages = goodsDao.selectShopsByConditionals(selectConditionalForm.getPage(), selectConditionalForm);
        //        将pages中的集合类型转成info
        List<Goods> oldRecords = pages.getRecords();
        List<GoodsInfo> goodsInfos = CopyProperties.copyList(oldRecords, GoodsInfo.class);
//        遍历集合，将其中的属性数组和鉴定图数组传入
        for (GoodsInfo goodsInfo : goodsInfos) {
            for (Goods goods : oldRecords) {
                if (goods.getGoodId() == goodsInfo.getGoodId()) {
                    goodsInfo.setGoodLdentificationFigure(GetObjectByJson.getList("", goods.getGoodLdentificationFigure(), LdentificationFigur.class));
                    goodsInfo.setGoodProperties(GetObjectByJson.getList("", goods.getGoodProperties(), PropertiesInfo.class));
                }
            }
        }
        pages.setRecords(goodsInfos);
        return pages;
    }

//    根据条件查询商品
    @Override
    public IPage<GoodsInfo> selectGoodsByCondition(ShopSelectConditionalForm shopSelectConditionalForm) {
//      设置条件
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
//      设置分页
        Page<Goods> goodsPage = new Page<>(shopSelectConditionalForm.getPage().getCurrent(), shopSelectConditionalForm.getPage().getSize());
//      判断条件
        if (shopSelectConditionalForm.getBrandId() != null && shopSelectConditionalForm.getBrandId() > 0) {
            wrapper.eq("brand_id", shopSelectConditionalForm.getBrandId());
        }
        if (shopSelectConditionalForm.getFirstTypeId() != null && shopSelectConditionalForm.getFirstTypeId() > 0) {
            wrapper.eq("good_first_type_id", shopSelectConditionalForm.getFirstTypeId());
        }
        if (shopSelectConditionalForm.getStoreId() != null && shopSelectConditionalForm.getStoreId() > 0) {
            wrapper.eq("store_id", shopSelectConditionalForm.getStoreId());
        }
        if (shopSelectConditionalForm.getGoodState() != null && shopSelectConditionalForm.getGoodState() != "") {
            wrapper.eq("good_state", shopSelectConditionalForm.getGoodState());
        }
        if (shopSelectConditionalForm.getSecondTypeId() != null && shopSelectConditionalForm.getSecondTypeId() > 0) {
            wrapper.eq("good_second_type_id", shopSelectConditionalForm.getSecondTypeId());
        }
        if (shopSelectConditionalForm.getWarehouseId() != null && shopSelectConditionalForm.getWarehouseId() > 0) {
            wrapper.eq("warehouse_id", shopSelectConditionalForm.getWarehouseId());
        }
        if (shopSelectConditionalForm.getVarValue() != null && shopSelectConditionalForm.getVarValue() != "" && shopSelectConditionalForm.getUserName() != null && shopSelectConditionalForm.getUserName() != "") {
            wrapper.and(wrapper1 -> wrapper1.like("brand_name", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("good_name", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("good_first_type", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("good_second_type", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("store_name", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("warehouse_name", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("user_name", shopSelectConditionalForm.getUserName()));
        } else if (shopSelectConditionalForm.getVarValue() == null || shopSelectConditionalForm.getVarValue() == "" && shopSelectConditionalForm.getUserName() != null && shopSelectConditionalForm.getUserName() != "") {
            wrapper.and(wrapper1 -> wrapper1.like("user_name", shopSelectConditionalForm.getUserName()));
        } else if (shopSelectConditionalForm.getVarValue() != null && shopSelectConditionalForm.getVarValue() != "" || shopSelectConditionalForm.getUserName() == null || shopSelectConditionalForm.getUserName() != "") {
            wrapper.and(wrapper1 -> wrapper1.like("brand_name", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("good_name", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("good_first_type", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("good_second_type", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("store_name", shopSelectConditionalForm.getVarValue())
                    .or()
                    .like("warehouse_name", shopSelectConditionalForm.getVarValue()));
        }
        wrapper.orderByDesc("submit_time");

//      查询
        Page<Goods> selectPage = goodsDao.selectPage(goodsPage, wrapper);

//      新建info集合
        ArrayList<GoodsInfo> goodsInfos = new ArrayList<>();

        IPage<GoodsInfo> goodsInfoIPage = new Page<>();
//       转换json
        for (Goods goods : selectPage.getRecords()) {
            List list = GetObjectByJson.getList("", goods.getGoodProperties(), PropertiesInfo.class);
            List list1 = GetObjectByJson.getList("", goods.getGoodLdentificationFigure(), LdentificationFigur.class);

            GoodsInfo goodsInfo = CopyProperties.copyOne(goods, GoodsInfo.class);
            goodsInfo.setGoodProperties(list);
            goodsInfo.setGoodLdentificationFigure(list1);
            goodsInfos.add(goodsInfo);
        }
//       重新设置page对象里面的records
        goodsInfoIPage.setRecords(goodsInfos);
        goodsInfoIPage.setTotal(selectPage.getTotal());
        goodsInfoIPage.setSize(selectPage.getSize());
        goodsInfoIPage.setCurrent(selectPage.getCurrent());

        return goodsInfoIPage;
    }

    @Override
    public Integer updateGoods(GoodsForm goodsForm) {
//        查找种类
        SecondType secondType = secondTypeDao.selectById(goodsForm.getGoodSecondTypeId());
        goodsForm.setGoodSecondType(secondType.getSecondTypeName());
//         查找二级种类
        FirstType firstType = firstTypeDao.selectById(goodsForm.getGoodFirstTypeId());
        goodsForm.setGoodFirstType(firstType.getFirstTypeName());
//          查找品牌
        Brand brand = brandDao.selectById(goodsForm.getBrandId());
        goodsForm.setBrandName(brand.getBrandName());

        Goods goods = CopyProperties.copyOne(goodsForm, Goods.class);
        goods.setGoodProperties(GetJson.getListJson("", goodsForm.getGoodProperties()));
        goods.setGoodLdentificationFigure(GetJson.getListJson("", goodsForm.getGoodLdentificationFigure()));

        int i = goodsDao.updateById(goods);
        return i;
    }
}
